package cn.net.msg.action;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringBufferInputStream;
import java.io.Writer;
import java.util.List;

import javax.annotation.Resource;

import cn.net.msg.model.User;
import cn.net.msg.service.UserService;

import com.opensymphony.xwork2.ActionSupport;
import com.sina.sae.storage.SaeStorage;
/*
 * 处理用户操作的Action类
 */
@SuppressWarnings("deprecation")
public class UserAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7574227471454774126L;
	private InputStream inputStream1;
	private User user;
	private List<User> list;
	@Resource(name="userService")
	UserService service;
	private int jieguo=0;
 	public String bb;
 	public StringBuilder filetext = new StringBuilder();
 	public boolean error;
 	
	public String inseart() {
		service.insertUser(user);
		return "success";
	}

	public String delete(){
		service.deleteById(user.getId());
		return "success";
	}
	
	public String  findByName(){
		setList(service.findByName(user.getName()));
		return "success";
	}
	
	public String update(){
		service.updateUser(user);
		return "success";
	}
	
	public String denglu() {
		boolean b;
		b=service.denglu(user);
		if(b)
		{
			jieguo=1;
			return "success";
		}
		else
		{
			jieguo=2;
		return "failure";
		}
	}
	
	public String xieru(){
		try
		{
		String lujing=new String(user.getBiaoti().getBytes("utf-8"),"gbk");
		String neirong;
		neirong=user.getNeirong();
		FileOutputStream outputStream = new FileOutputStream("saestor://travelnote/"+lujing+".txt");
		Writer writer = new OutputStreamWriter(outputStream, "gbk");
		writer.write(neirong);
		writer.close();
		outputStream.close();
		jieguo=4;
		return "success";
		}
		catch(Exception e)
		{
			jieguo=2;
			return "failure";
		}
	}
	
	public String duqu(){
		
		try{
			String lujing;
			lujing=user.getBiaoti();
			//读storage中域名为domain，文件名为test.txt的文件
		    FileInputStream inputStream = new FileInputStream("saestor://travelnote/"+lujing+".txt");
			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
			String tempchar;
			while ((tempchar = new String(reader.readLine().getBytes("utf-8"),"utf-8")) != null) {
			   filetext.append(tempchar);
			}
			reader.close();
			jieguo=5;
			if(filetext.length()==0)
			{
				jieguo=6;
			}
			return "success";
		}
		catch(Exception e){
			jieguo=2;
			return "failure";
		}
	}
	
	public String shanchu()
	{
		String lujing;
		lujing=user.getBiaoti();
		try
		{
			SaeStorage wenjian=new SaeStorage();
			error=wenjian.delete("travelnote",lujing);
			jieguo=4;
			if(!error)
			jieguo=7;
			return "success";
		}
		catch(Exception e)
		{
			jieguo=2;
			return "failure";
		}
	}
	
	public String findAll(){
		setList(service.findAll());
		return "success";
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getList() {
		return list;
	}

	public void setList(List<User> list) {
		this.list = list;
	}
	/*
	 * 校验插入数据的操作
	 */
	public void validateInseart(){
		if(null==user.getName()||"".equals(user.getName().trim())) 
			this.addFieldError("name", "name can`t be null");
		else if(16<user.getName().trim().length()) 
			this.addFieldError("name", "name is limited in 16 characters");		
		if(null==user.getPassword()||"".equals(user.getPassword().trim()))
			this.addFieldError("password", "value of password is unvalid");
	}
	/*
	 * 校验更新数据的操作
	 */
	public void validateUpdate(){
		if(100000<user.getId())
			this.addFieldError("id", "value of id is unvalid");
		
		if(null==user.getName()||"".equals(user.getName().trim())) 
			this.addFieldError("name", "name can`t be null");
		else if(16<user.getName().trim().length()) 
			this.addFieldError("name", "name is limited in 16 characters");
		if(null==user.getPassword())
			this.addFieldError("password", "value of password is unvalid");
	}
	/*
	 * 校验删除数据的操作
	 */
	public void validateDelete(){
		if(100000<user.getId())
			this.addFieldError("id", "value of id is unvalid");
	}
	public void validateFindByName(){
		if(null==user.getName()||"".equals(user.getName().trim())) 
			this.addFieldError("name", "name can`t be null");
		else if(16<user.getName().trim().length()) 
			this.addFieldError("name", "name is limited in 16 characters");
	}
	

	 public InputStream getInputStream() {
		 	switch(jieguo)
		 	{
		 	case 0:bb="null";break;
		 	case 1:bb="ok";break;
		 	case 2:bb="no";break;
		 	case 4:bb="right";break;
		 	case 5:bb=filetext.toString();break;
		 	case 6:bb="wu";break;
		 	case 7:bb="no find";break;
		 	default:
		 	}
		 	inputStream1 = new StringBufferInputStream(bb);
	        return inputStream1;
	    }
	 
}
